package com.ithiema.reggie.Service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithiema.reggie.Mapper.CategoryMapper;
import com.ithiema.reggie.Service.CategoryService;
import com.ithiema.reggie.Service.DishService;
import com.ithiema.reggie.Service.SetmealService;
import com.ithiema.reggie.common.CustomException;
import com.ithiema.reggie.entity.Category;
import com.ithiema.reggie.entity.Dish;
import com.ithiema.reggie.entity.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private  DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
    public void deleteid(long id) {

//        1.根据当前分类的ID，查询该分类下是否存在菜品，如果存在，则提示错误信息
        LambdaQueryWrapper<Dish> lwp1 = new LambdaQueryWrapper<>();
        lwp1.eq(Dish::getCategoryId,id);
        int count = dishService.count(lwp1);
        if (count>0){
            throw new CustomException("删除不了有菜品");
        }

//        2.根据当前分类的ID，查询该分类下是否存在套餐，如果存在，则提示错误信息
        LambdaQueryWrapper<Setmeal> lwp2 = new LambdaQueryWrapper<>();
        lwp2.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(lwp2);
        if (count1>0){
            throw new CustomException("删除不了有套餐");
        }
//        3.执行正常的删除分类操作
        removeById(id);

    }
}
